//
//  CellForRadioList.m
//  世界
//
//  Created by admin on 16/2/14.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "CellForRadioList.h"
#import "CellForRadioType.h"
#import <UIImageView+WebCache.h>

@interface CellForRadioList ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UIView *viewForSeparator1;
@property (nonatomic, retain) UIView *viewForSeparator2;

@end

@implementation CellForRadioList


#pragma mark 重写 dealloc 方法
- (void)dealloc
{
    [_arrCname release];

    [_arrTname release];

    [_arrTitle release];
    
    [_arrImgsrc release];
    
    [_lableForCname release];
    
    [_buttonForEnter release];
    
    [_arrImgsrc release];
    
    [_arrTitle release];
    
    [_flowLayout release];
    
    [_collection release];
    
    [_viewForSeparator1 release];
    
    [_viewForSeparator2 release];
    
    [_arrPostid release];
    
    [_arrTid release];
    
    [super dealloc];
}


#pragma mark 重写初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self != nil)
    {
        [self createCollectionView];
        
        
        self.lableForCname = [[UILabel alloc] init];
        
        [self.contentView addSubview:self.lableForCname];
        
        [_lableForCname release];
        
        [self.lableForCname setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
        
        
        self.buttonForEnter = [UIButton buttonWithType:UIButtonTypeSystem];
        
        
        [self.buttonForEnter setTitle:@"更多" forState:UIControlStateNormal];
        
        [self.buttonForEnter setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [self.buttonForEnter addTarget:self action:@selector(buttonForEnterAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:self.buttonForEnter];
        
        [_buttonForEnter release];
        
        
        self.viewForSeparator1 = [[UIView alloc] init];
        
        [self.contentView addSubview:self.viewForSeparator1];
        
        self.viewForSeparator1.backgroundColor = [UIColor lightGrayColor];
        
        [_viewForSeparator1 release];
        
        
        self.viewForSeparator2 = [[UIView alloc] init];
        
        [self.contentView addSubview:self.viewForSeparator2];
        
        self.viewForSeparator2.backgroundColor = [UIColor lightGrayColor];
        
        [_viewForSeparator2 release];
    }
    return self;
}


#pragma mark 点击button所触发的方法
- (void)buttonForEnterAction:(UIButton *)button
{
    NSString *strForURL = self.arrCid[self.count];
    
    NSString *strForCname = self.arrCname[self.count];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Radio" object:nil userInfo:@{@"strForURL" : strForURL, @"Cname" : strForCname}];
}



#pragma mark 布局子视图
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.collection.frame = CGRectMake(0, 50, self.contentView.frame.size.width, self.contentView.frame.size.height - 50);
    
    self.flowLayout.itemSize = CGSizeMake(self.contentView.frame.size.width/(self.arrTitle.count/self.arrCname.count), self.contentView.frame.size.height - 50);
    
    self.viewForSeparator1.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 3);
    
    self.viewForSeparator2.frame = CGRectMake(0, 49, self.contentView.frame.size.width, 1);
    
    self.lableForCname.frame = CGRectMake(20, 10, 200, 30);
    
    self.buttonForEnter.frame = CGRectMake(350, 10, 50, 30);
}





#pragma mark 自定义方法 >>>>>>
#pragma mark 设置布局格式
//FlowLayout父类是UIcollectionViewLayout
- (void)createFlowLayout
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //item之间最小间距(横向)
    self.flowLayout.minimumInteritemSpacing = 0;
    
    //滑动方向
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}




#pragma mark 创建CollectionView
- (void)createCollectionView
{
    [self createFlowLayout];
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.contentView.frame.size.width, self.contentView.frame.size.height - 50) collectionViewLayout:self.flowLayout];
    
    [_flowLayout release];
    
    self.collection.backgroundColor = [UIColor whiteColor];
    
    self.collection.showsVerticalScrollIndicator = NO;
    
    [self.contentView addSubview: self.collection];
    
    [_collection release];
    
    
    //指定代理人
    self.collection.dataSource = self;
    
    self.collection.delegate = self;
    
    //注册Cell(collectionView 只支持先注册)
    [self.collection registerClass:[CellForRadioType class] forCellWithReuseIdentifier:@"pool1"];
}



#pragma mark UICollectionViewDataSource 的协议方法 >>>>>>
#pragma mark 返回Item的个数 (required 1/2)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrTitle.count / self.arrCname.count;
}




#pragma mark 给相应的Item上添加Cell (required 2/2)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CellForRadioType *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    //cell赋值
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.arrImgsrc[self.count * (self.arrTitle.count / self.arrCname.count) + indexPath.item]] placeholderImage:[UIImage imageNamed:@"加载中"]];
    
    
    cell.lableForTitle.text = self.arrTitle[self.count * (self.arrTitle.count / self.arrCname.count) + indexPath.item];
    
    cell.lableForTname.text = self.arrTname[self.count * (self.arrTitle.count / self.arrCname.count) + indexPath.item];
    
    return cell;
}






#pragma mark UICollectionViewDelegate 的协议方法 >>>>>>
#pragma mark 选择相应的Item所触发的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *strForNumber = [NSString stringWithFormat:@"%ld", indexPath.row];
    
    NSString *strForTname = self.arrTname[self.count * (self.arrTitle.count / self.arrCname.count) + indexPath.item];
    
    NSString *strForTid = self.arrTid[self.count * (self.arrTitle.count / self.arrCname.count) + indexPath.item];
    
    NSString *strForPostid = self.arrPostid[self.count * (self.arrTitle.count / self.arrCname.count) + indexPath.item];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RadioPlay" object:nil userInfo:@{@"number" : strForNumber, @"tid" : strForTid, @"postid" : strForPostid, @"tname" : strForTname}];
}




- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}


@end
